﻿using NanoVirtualView;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 图片裁剪压缩工具
{
    public class RectView : BaseRectView
    {
        //public enum RectType { Free, ConstAspectRatio, ConstSize }
        public IRectEvent CurrentRectEvent { private set; get; }

        public RectView(VirtualViewRegister register,SizeF size=new SizeF()) : base(register)
        {
            Location = new PointF(100, 100);
            Size =size!=new SizeF()?size: new SizeF(200, 200);
            
        }

        private void RemoveEvent(IRectEvent rectEvent) 
        {
            MouseDown -= rectEvent.MouseDown;
            MouseUp -= rectEvent.MouseUp;
            MouseLeave -= rectEvent.MouseLeave;
            MouseMove -= rectEvent.MouseMove;
        }
        private void AddEvent(IRectEvent rectEvent)
        {
            MouseDown += rectEvent.MouseDown;
            MouseUp += rectEvent.MouseUp;
            MouseLeave += rectEvent.MouseLeave;
            MouseMove += rectEvent.MouseMove;
        }
        private void ChangeEventTo(IRectEvent rectEvent)
        {
            if(CurrentRectEvent != null)
            {
                RemoveEvent(CurrentRectEvent);
            }
            else
            {
                AddEvent(rectEvent);
                CurrentRectEvent = rectEvent;
            }
        }
        private void ChangeUI(SizeF size,PointF point)
        {
            if (size != new SizeF())
            {
                Size = size;
            }
            if(point!=new PointF())
            {
                Location = point;
            }
        }
        public void ChangeEventToAndChangeUI(IRectEvent rectEvent, SizeF size=new SizeF(),PointF location=new PointF())
        {
            ChangeEventTo(rectEvent);
            ChangeUI(size,location);
        }
        public override void OnEndPaint()
        {
           
        }

        public override void OnPaint(PaintEventArgs e)
        {
            var rect = Rectangle;
            e.Graphics.DrawRectangle(Pens.Black, rect.X,rect.Y,rect.Width,rect.Height);
        }

        public override void OnPrePaint()
        {
            
        }
    }
}
